<?php
namespace Scan;
use Model\MemberMod;
use Model\WqMemberMod;

class Scan
{
    protected $Member = null;

    protected $_scan_uid = 0;  //扫码的用户id
    protected $_dispose_uid = 0;// 被扫码的用户id
    protected $_activity_id = 0;  //活动id
    protected $_uniacid = 0;  //公众号id
    protected $_nickname = '';  //扫码用户的昵称
    protected $_header = '';   //扫码用户的头像
    protected $_result = array('status'=>'error','err_msg'=>'未知错误，请重试');
    public function __construct()
    {
        global $_GPC,$_W;
        $this->_uniacid = $_W['account']['uniacid'];
        if(isset($_GPC['uid'])) $this->_dispose_uid = $_GPC['uid'];
        if(isset($_GPC['activity_id'])) $this->_activity_id = $_GPC['activity_id'];
        $this->register();
    }

    /**
     * 扫码的时候自动注册用户
     */
    private function register()
    {
        global $_W;
        checkauth();
        $this->_scan_uid = $_uid = $_W['member']['uid'];
        $WqMember = new WqMemberMod();
        $_wq_member = $WqMember->findOne(array('uid'=>$_uid),array('nickname','avatar'));
        if($_wq_member['nickname'] == '' || $_wq_member['avatar'] == ''){
            $WxApi = \WeAccount::create();
            $_wx_member = $WxApi->fansQueryInfo($_W['openid']);
            $_wq_member['nickname'] = $_wx_member['nickname'];
            $_wq_member['avatar'] = $_wx_member['headimgurl'];
        }
        $this->_nickname = $_wq_member['nickname'];
        $this->_header = $_wq_member['avatar'];

        $Member = new MemberMod();
        $_member = $Member->findOne(array('uniacid'=>$_W['account']['uniacid'],'uid'=>$_uid),array('id'));
        if(!$_member){
            $Member->insert(array(
                'uniacid'=>$_W['account']['uniacid'],
                'uid'=>$_uid
            ));
        }
    }
}